import { fetchList, fetchCates, fetchSetting, fetchBanners, fetchRecommends, fetchLinks } from './api'

export default {
  namespaced: true,
  state: () => ({
    list: [],
    total: 0,
    cates: [],
    banners: [],
    setting: {},
    recommends: []
  }),
  getters: {
    list (state) {
      return state.list
    },
    total (state) {
      return state.total
    },
    cates (state) {
      return state.cates
    },
    banners (state) {
      return state.banners
    },
    recommends (state) {
      return state.recommends
    },
    links (state) {
      return state.links
    },
    setting (state) {
      return state.setting
    },
    repair (state) {
      return state.setting.repair
    },
    logo (state) {
      return state.setting.logo;
    },
    title (state) {
      return state.setting.title;
    },
    keywords (state) {
      return state.setting.keywords;
    },
    description (state) {
      return state.setting.description;
    },
    comment (state) {
      return state.setting.comment;
    },
    website (state) {
      const { nick_name, signature, avatar, visibility, contact, record, record_url } = state.setting
      return { nick_name, signature, avatar, visibility, contact, record, record_url }
    }
  },
  actions: {
    async fetchList ({ commit }, params) {
      return new Promise((resolve, reject) => {
        fetchList(params).then((res) => {
          commit('SET_LIST', res.data);
          resolve(res.data);
        }).catch((err) => {
          reject(err);
        });
      });
    },
    async fetchCates ({ commit }, params) {
      const { data } = await fetchCates(params)
      commit('SET_CATES', data)
    },
    async fetchBanners ({ commit }) {
      return new Promise((resolve, reject) => {
        fetchBanners().then((res) => {
          commit('SET_BANNERS', res.data);
          resolve(res.data);
        }).catch((err) => {
          reject(err);
        });
      });
    },
    async fetchRecommends ({ commit }) {
      return new Promise((resolve, reject) => {
        fetchRecommends().then((res) => {
          commit('SET_RECOMMENDS', res.data);
          resolve(res.data);
        }).catch((err) => {
          reject(err);
        });
      });
    },
    async fetchLinks ({ commit }) {
      return new Promise((resolve, reject) => {
        fetchLinks().then((res) => {
          commit('SET_LINKS', res.data);
          resolve(res.data);
        }).catch((err) => {
          reject(err);
        });
      });
    },
    async fetchSetting ({ commit }) {
      const { data } = await fetchSetting()
      commit('SET_SETTING', data)
    },
  },
  mutations: {
    SET_LIST (state, payload) {
      state.list = payload.data
      state.total = payload.meta.total
    },
    SET_CATES (state, payload) {
      state.cates = payload.data
    },
    SET_BANNERS (state, payload) {
      state.banners = payload.data
    },
    SET_RECOMMENDS (state, payload) {
      state.recommends = payload.data
    },
    SET_LINKS (state, payload) {
      state.links = payload.data
    },
    SET_SETTING (state, payload) {
      state.setting = payload.data
    }
  }
}
